package com.github.dongglin.smart.spring.boot.plus.common.exception;

import com.github.dongglin.smart.spring.boot.plus.common.enums.BusinessCode;
import com.github.dongglin.smart.spring.boot.plus.common.enums.StatusCode;

public class BizPreconditions {
    /**
     * 异常检查
     *
     * @param b            检查条件
     * @param businessCode 业务异常码
     */
    public static void checkSpotState(final boolean b, final BusinessCode businessCode) {
        if (b) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 异常检查
     *
     * @param b   检查条件
     * @param msg 错误消息
     */
    public static void checkSpotState(final boolean b, final String msg) {
        if (b) {
            throw new BusinessException(StatusCode.E0000000.getCode(), msg);
        }
    }

    /**
     * @param b            b
     * @param businessCode businessCode
     * @param data         data
     */
    public static void checkSpotState(final boolean b, final BusinessCode businessCode, Object data) {
        if (b) {
            throw new BusinessException(businessCode, data);
        }
    }

    /**
     * @param b b
     */
    public static void checkSpotState(final boolean b) {
        if (b) {
            throw new RuntimeException();
        }
    }

    /**
     * @param b            b
     * @param businessCode businessCode
     */
    public static void checkInnerSpot(final boolean b, final BusinessCode businessCode) {
        if (b) {
            throw new InnerException(businessCode);
        }
    }

    /**
     * @param e e
     * @return 业务异常
     */
    public static BusinessException convert(InnerException e) {
        return new BusinessException(e.getCode(), e.getMessage());
    }

}
